// 处理上传附件
export const filterUploadList = (list = []) => {
  const fileList = {}
  list.forEach(element => {
    let processId = element.processId
    const proceeList = fileList[processId] || []
    proceeList.push(element)
    fileList[processId] = proceeList
  })
  return fileList
}

export const filterRouter = (userRouter = [], allRouter = []) => {
  const roleFilterArr = allRouter
    .filter(item => userRouter.includes(item.name)) // 过滤第一级路由
    .map(item => ({
      ...item,
      children: item.children ? filterRouter(userRouter, item.children) : null
    }))
  return roleFilterArr
}

// 菜单过滤
export const filterRouterByParent = (menus = [], parentId) => {
  const newMenus = menus.filter((item) => {
    if (item.parentId === parentId && item.type !== 2) {
      item.children = filterRouterByParent(menus, item.menuId)
      return item
    }
  })
  return newMenus
}

// 路由菜单
export const mapRouter = (router, parentUrl = 'page') => {
  const routers = router.map(item => {
    const { url, menuName, icon, routerName, children } = item
    const meta = { title: menuName, icon }
    const CommonRoute = () => import('@/layout/components/CommonRoute')
    return {
      path: url,
      meta,
      name: routerName,
      children: mapRouter(children, url === 'common' ? url : null),
      component: children.length !== 0 ? CommonRoute : () => import('@/' + (parentUrl === null ? 'page' : parentUrl) + '/' + routerName)
    }
  })
  return routers
}

//
export const renderTable = (list, key, code) => {
  const table = {}
  const getArrDeep = (arr) => {
    let res = 1;
    (function myDeep(arr, num) {
      arr.forEach((item) => {
        const children = item.children
        if (children && children.length !== 0) {
          myDeep(children, num + 1)
        } else {
          res = res < num + 1 ? num + 1 : res
        }
      })
    })(arr, 1)
    return res
  }

  // 获取子叶深度
  // 用户获取父级col值
  const getLeaf = (arr) => {
    let res = 0;
    (function getLeaf(arr) {
      arr.forEach((item) => {
        if (item.children && item.children.length !== 0) {
          getLeaf(item.children)
        } else {
          res += 1
        }
      })
    })(arr)
    return res
  }

  // 判断数组
  const existDataOfArray = (arr) => arr instanceof Array && arr.length !== 0

  // table处理
  const mapTableData = (data, index, parentDeep, key) => {
    index = index || 1
    data.forEach((item) => {
      const children = item.children
      let nodeDeep = getArrDeep(children)
      let nodeLeaf = getLeaf(children)
      const childrenIsArray = existDataOfArray(children)
      if (childrenIsArray) {
        mapTableData(children, index + 1, parentDeep - 1, key)
      }
      pushTableObj(index,
        {
          key: item[key],
          row: childrenIsArray ? 1 : parentDeep - nodeDeep,
          col: nodeLeaf,
          code: item[code]
        }
      )
    })
  }

  const pushTableObj = (index, obj) => {
    if (!table.hasOwnProperty(index)) {
      table[index] = []
    }
    table[index].push(obj)
  }

  // 最深
  var dataDeep = getArrDeep(list)
  // 绘制table
  mapTableData(list, 1, dataDeep, key)

  return table
}
